package main;

import java.text.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

//Nikolas Wolfe
//4/13/09
//CIS 4930
//Game of Life 2

/* =============================================================================
* Registers statistics 
*/

public abstract class Statistics
{
	private final ArrayList<String> history;
	private final ArrayList<Double> executionTimes;
	private StringBuilder simulationSummary;
	private long startTime;
	private long stopTime;
	private final String units;
	
	public Statistics()
	{
		this.history = new ArrayList<String>();
		this.executionTimes = new ArrayList<Double>();
		this.simulationSummary = new StringBuilder("");
		this.startTime = 0;
		this.stopTime = 0;
		this.units = " us";
	}
	
	/* =============================================================================
	 * Clear current simulation report
	 */
	public void flushSummary()
	{
		executionTimes.add( getElapsedTime() );
		history.add( simulationSummary.toString() );
		simulationSummary = new StringBuilder("");
		startTime = 0;
		stopTime = 0;
	}
	
	/* =============================================================================
	 * Report Compilation
	 */
	public String getCurrentReport()
	{
		return simulationSummary.toString();
	}
	
	public ArrayList<String> getHistory()
	{
		return history;
	}
	
	public ArrayList<Double> getExecutionHistory()
	{
		return executionTimes;
	}
	
	public double getAverageExecutionTime()
	{
		double averageExecutionTime = 0;
		Iterator<Double> i = executionTimes.iterator();
		while( i.hasNext() )
		{
			averageExecutionTime += i.next();
		}
		averageExecutionTime = (averageExecutionTime / executionTimes.size());
		return averageExecutionTime;
	}
	
	/* =============================================================================
	 * Accessor / Mutator methods
	 */
	protected double getElapsedTime()
	{
		double start = TimeUnit.MICROSECONDS.convert(startTime, TimeUnit.NANOSECONDS);
		double stop = TimeUnit.MICROSECONDS.convert(stopTime, TimeUnit.NANOSECONDS);
		return Math.abs( stop - start );
	}
	
	protected void setStartTime( long start )
	{
		this.startTime = start;
	}
	
	protected void setStopTime( long stop )
	{
		this.stopTime = stop;
	}
	
	protected void append( String s )
	{
		simulationSummary.append( s );
	}

	public String getUnits()
	{
		return units;
	}
}
